package com.chy.utilsdemo.FileUtils;


import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

/**
 * Id生成工具类
 * @author linmh
 */
public class IdGeneratorUtil {
    /**
     * 生成36位UUID
     * @return string 36位uuid
     */
    public static String UUIDGenerate() {
        return UUID.randomUUID().toString();
    }

    /**
     * 生成32位大写UUID
     * @return string 36位uuid
     */
    public static String UUID32Generate() {
        return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }


    /**
     * 生成12位伪随机的Id。
     * xxx(当前日期是一年中的第几天。三位数)+xx(当前小时。两位数)+xx(当前分钟。两位数)+xx(当前秒。两位)+xxx(随机字符，可能是数字或者大写、小写字母。)
     * @return String 12位随机数
     */
    public static String randomIDGenerate() {
        return getRandomIdWithYear(new StringBuffer());
    }


    /**
     * 生成指定长度的随机伪随机Id。可能是数字或者大写、小写字母。
     *
     * @param length Id长度
     * @return String 指定长度id
     **/
    public static String idIDGenerate(int length) {
        return getRandomId(new StringBuffer(), length);
    }


    private static String getRandomIdWithYear(StringBuffer idBuffer) {
        Calendar calendar = Calendar.getInstance();
        idBuffer.append(getFormatDayOfYear(calendar.get(Calendar.DAY_OF_YEAR)));
        idBuffer.append(getFormatTime(calendar.get(Calendar.HOUR)));
        idBuffer.append(getFormatTime(calendar.get(Calendar.MINUTE)));
        idBuffer.append(calendar.get(Calendar.SECOND));
        idBuffer.append(getRandomChar());
        idBuffer.append(getRandomChar());
        idBuffer.append(getRandomChar());
        return idBuffer.toString();
    }


    private static String getRandomId(StringBuffer idBuffer, int length) {
        for (int index = 0; index < length; index++) {
            idBuffer.append(getRandomChar());
        }
        return idBuffer.toString();
    }

    private static String getFormatDayOfYear(int timeResult) {
        StringBuilder format = new StringBuilder();
        if (timeResult < 10) {
            format.append("00");
        } else if (timeResult < 100) {
            format.append("0");
        }
        format.append(timeResult);
        return format.toString();
    }

    private static String getFormatTime(int timeResult) {
        StringBuilder format = new StringBuilder();
        if (timeResult < 10) {
            format.append("0");
        }
        format.append(timeResult);
        return format.toString();
    }

    private static Object getRandomChar() {
        double randomKey = Math.random();
        if (randomKey < 0.3) {
            return (char) (randomKey * 26 + 97);
        } else if (randomKey > 0.6) {
            return (char) (randomKey * 26 + 65);
        } else {
            return Math.round(randomKey * 10);
        }
    }

    /**
     * 随机生成5位数
     */
    private static String randomNumber(){
        String str = "";
        Random rand = new Random();
        for(int i=0;i<5;i++){
            int num = rand.nextInt(3);
            switch(num){
                case 0:
                    char c1 = (char)(rand.nextInt(26)+'a');//生成随机小写字母
                    str += c1;
                    break;
                case 1:
                    char c2 = (char)(rand.nextInt(26)+'A');//生成随机大写字母
                    str += c2;
                    break;
                case 2:
                    str += rand.nextInt(10);//生成随机数字
            }
        }
        return str;
    }

    /**
     * 生成唯一的主键
     * 格式：时间+随机数
     * @return
     */
    public static synchronized String genUniqueKey(){
        Random random=new Random();
        Integer number=random.nextInt(900000)+100000;

        return System.currentTimeMillis()+String.valueOf(number);
    }

}
